package com.liser.material.design.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.king.zxing.Intents;
import com.liser.material.design.MainActivity;
import com.liser.material.design.R;
import com.liser.material.design.framework.helper.ToastHelper;
import com.liser.material.design.framework.ui.BaseFragment;
import com.liser.material.design.hardware.GprinterService;
import com.liser.material.design.net.Network;
import com.liser.material.design.net.NetworkSubs;
import com.liser.material.design.net.exception.ExceptionAction;
import com.liser.material.design.net.model.GoodsPrintResult;
import com.liser.material.design.ui.qrcode.QRCodeActivity;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomeFragment extends BaseFragment {

    private static final String TAG = HomeFragment.class.getName();

    @BindView(R.id.rv_goods)
    RecyclerView goodsRecyclerView;

    private GoodsLinearItemAdapter goodsLinearItemAdapter;

    @Override
    protected int onLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void onView() {
        initRecyclerView();
    }

    private void initRecyclerView() {
        VirtualLayoutManager connectedManager = new VirtualLayoutManager(getContext());
        LayoutHelper goodsHelper = new LinearLayoutHelper();
        goodsRecyclerView.setLayoutManager(connectedManager);
        // 分割线
        goodsRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        goodsLinearItemAdapter = new GoodsLinearItemAdapter(getActivity(), goodsHelper);
        goodsRecyclerView.setAdapter(goodsLinearItemAdapter);
    }

    @OnClick(R.id.sweepCode)
    public void onSweepCodeClick(View view) {
        // 跳转到扫码界面
        startActivityForResult(new Intent(getActivity(), QRCodeActivity.class), MainActivity.REQUEST_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (MainActivity.REQUEST_CODE == requestCode && Activity.RESULT_OK == resultCode) {
            if (data != null) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                String result = bundle.getString(Intents.Scan.RESULT);
                Log.i(TAG, "扫码结果：" + result);
                netWork();
            }
        }
    }

    // fragment页面间传值
    /*@Override
    public void onActivityCreated(@Nullable Bundle bundle) {
        super.onActivityCreated(bundle);
        if (getArguments() != null && !TextUtils.isEmpty(getArguments().getString(Intents.Scan.RESULT))) {
            String qrcode = getArguments().getString(Intents.Scan.RESULT);
            Log.i(TAG, "扫码结果：" + qrcode);
            netWork();
        }
    }*/

    @OnClick(R.id.goodsPrint)
    public void onGoodsPrintClick(View view) {
        if (goodsLinearItemAdapter.getData().size() > 0) {
            GprinterService.getInstance()
                    .printBill(goodsLinearItemAdapter.getData())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Integer>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(Integer integer) {
                            ToastHelper.show("打印成功条数：" + integer);
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    private void netWork() {
        Network.api()
                .goodsPrint("010017", "0023")
                .compose(bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetworkSubs<GoodsPrintResult>() {
                    @Override
                    protected void onSuccess(GoodsPrintResult data) {
                        goodsLinearItemAdapter.addData(data);
                    }
                });
    }
}